// component/LuoAn/DeviceCell/DeviceAddressDetail.js

const app = getApp()
const {LuoAnHttp} = app.globalData;
const marker = {
  latitude: 0,
  longitude: 0,
  title: '',
  width: 18,
  height: 24,
  iconPath: '/img/common/location_icon.png',
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    model:{},
    latitude:-1,
    longitude:-1,
    marker:marker
  },
  onShow: function () {
    
  },
  
  cancelAlarm:async function(e){
    const that = this;
    const eventChannel = this.getOpenerEventChannel()
    wx.showModal({
      cancelColor: '#666666',
      confirmColor:'#0F93F2',
      title:'解除警报',
      content:'是否解除警报',
      success:async s=>{
        if(s.confirm){
          const param = {
            eqid:that.data.model.eqid,
            deviceid:that.data.model.deviceid,
            manual:'Y'
          }
          const res = await LuoAnHttp.asyncDissmissAlarm(param)
          if(res&&res.code==200){
            eventChannel&&eventChannel.emit('callback',{})
            that.setData({
              model:{...this.data.model,manual:'Y'}
            })
          }
        }
      }
    })
    
  },
  checkPath:function(e){
    wx.navigateTo({
      url: './deviceMovePath',
      events: {
        callback: f => {},
      },
      success: ({
        eventChannel
      }) => {
         
        eventChannel.emit('param', this.data.model)
      }
    })
  },
  updateLocation:async function(e){
    const eventChannel = this.getOpenerEventChannel()
    const that = this;
    wx.navigateTo({
      url: './LocationChoice',
      events:{
        callback:async (e)=>{
           
          const param = {
            ...e,
            eqid:that.data.model.eqid,
          }
          const res2 = await LuoAnHttp.asyncUpdateDeviceLocation(param);
          if(res2){
             
            eventChannel&&eventChannel.emit('callback');
            that.popMethod()
            that.setData({
              model:{...that.data.model,deviceposition:param.deviceposition,longitudeandlatitude:`${e.longitude},${e.latitude}`,lastLatitude:e.latitude,longitude:e.longitude},
            })
          }
        }
      }
    }) 
  },
  openWXLocation:function(e){
    wx.openLocation({
      latitude:parseFloat(this.data.lastLatitude) ,
      longitude:parseFloat(this.data.lastLongitude) ,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    const eventChannel = this.getOpenerEventChannel()
    // 请求上一个页面传参数过来
    eventChannel&&eventChannel.on('param', function(data) {
      let arr = [0,0];
      if(data.longitudeandlatitude){
        arr = data.longitudeandlatitude.split(',');
      }
      that.setData({
        model:data,
        lastLatitude:parseFloat(arr[1]) ,
        lastLongitude:parseFloat(arr[0]),
        marker:{...marker,longitude:parseFloat(arr[0]),latitude:parseFloat(arr[1])}
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  popMethod:function(e){
    let pages = getCurrentPages();
    if(pages.length>1){
      wx.navigateBack();
    }else{
      wx.reLaunch({
        url: '/pages/luoAn/device/deviceIndex',
      })
    }
  }
})